package com.nbwyctf.helper.utils;

import org.apache.logging.log4j.util.Strings;

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author chetaifu
 */
public class TimeTransformUtil {
    public static final String LOCAL_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String LOCAL_DATE_PATTERN = "yyyy-MM-dd";
    public static final String LOCAL_TIME_PATTERN = "HH:mm:ss";

    /** Date 转 LocalDateTime */
    public static LocalDateTime toLocalDateTime(Date date) {
        /* 方法一: LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); */
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /** LocalDateTime 转 Date */
    public static Date toDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /** String 转 LocalDateTime */
    public static LocalDateTime toLocalDateTime(String str) {
        // 处理带纳秒的字符串，例如：'2021-03-11 14:49:24.997'
        if (str.indexOf('.') != -1) {
            str = str.substring(0, str.indexOf('.'));
        }
        return LocalDateTime.parse(str, DateTimeFormatter.ofPattern(LOCAL_DATETIME_PATTERN));
    }

    /** String 转 LocalDate */
    public static LocalDate toLocalDate(String dateStr) {
        return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(LOCAL_DATE_PATTERN));
    }

    /** LocalDateTime 转 String */
    public static String localDateTimeToStr(LocalDateTime dateTime, String pattern) {
        if (dateTime == null) {
            return null;
        }
        if (Strings.isBlank(pattern)) {
            pattern = LOCAL_DATETIME_PATTERN;
        }
        return dateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    /** LocalDate 转 String */
    public static String localDateToStr(LocalDate date) {
        if (date == null) {return null;}
        return date.format(DateTimeFormatter.ofPattern(LOCAL_DATE_PATTERN));
    }

    /** timestamp(时间戳) 转 LocalDateTime */
    public static LocalDateTime toLocalDateTime(long timestamp) {
        Instant instant = Instant.ofEpochMilli(timestamp);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }

    /** dateTime 转 timestamp(时间戳) */
    public long toTimestamp(LocalDateTime dateTime){
        return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /** LocalDate 转 当日0时0分0秒 的 LocalDateTime */
    public static LocalDateTime startOfDay(LocalDate date) {
        return date.atStartOfDay();
    }

    /** 将LocalDateTime 转 当日0时0分0秒 的 LocalDateTime */
    public static LocalDateTime startOfDay(LocalDateTime datetime) {return datetime.withHour(0).withMinute(0).withSecond(0).withNano(0);}

    /** 将LocalDate 转 当日23时59分59秒 的 LocalDateTime*/
    public static LocalDateTime endOfDay(LocalDate date) {
        return date.plusDays(1).atStartOfDay().minusSeconds(1);
    }

    /** 将LocalDateTime 转 当日23时59分59秒 的 LocalDateTime */
    public static LocalDateTime endOfDay(LocalDateTime datetime) {
        return startOfDay(datetime).plusDays(1).minusSeconds(1);
    }
}
